#include "Log.h"
#include <ctime>


LogInformation::LogInformation(cchar* file , cchar* func , uint32 line , int64 xtime  , uint32 xthread , uint32 process ): 
m_file( file ),
m_line( line ),
m_function(func),
m_thread(xthread),
m_process(process),
m_time(xtime)
{
	if( 0 == process  )
		m_process = GetCurrentProcessId();
	if( 0 == m_thread )
		m_thread  = GetCurrentThreadId();
	if( 0 == m_time   )
		m_time    = time(0);
}

LogInformation::LogInformation()
{
	m_process  = GetCurrentProcessId();
	m_thread   = GetCurrentThreadId();
	m_time     = time(0);
}

int32 GobalLog::Add( Logger* logger , point argument )
{
	if( logger == NULL )
		return E_NULL_LOGGER;

	int32 code =  logger->Init( argument );
	if( code != R_SUCCEED )
		return code;

	m_loggers.push_back( logger );
	
	return R_SUCCEED;
}

int32 GobalLog::Log( LogInformation& logInformation )
{
	iterator first = m_loggers.begin();
	iterator last  = m_loggers.end();

	if( first == last )
		return E_EMPTY_LOGGER;
	int32 code = R_SUCCEED;
	for( ; first != last ; ++first )
	{
		int32 currentCode = (*first)->Log( logInformation );
		if( currentCode != R_SUCCEED )
			code = currentCode;
	}

	return code;
}


GobalLog* gobalLog = 0;

GobalLog* GetGobalLog()
{
	if( gobalLog == 0)
		gobalLog = new GobalLog();
	return gobalLog;
}

GobalLog* AddLogger( Logger* logger )
{
	GobalLog* log = GetGobalLog();
	log->Add(logger , 0 );
	return log;
}